﻿using UnityEditor;
using UnityEngine;

namespace EditorExtension
{
    public class GenericMenuExample : EditorWindow
    {
        [MenuItem("EditorExtensions/08_GenericMenu/Open")]
        static void Open()
        {
            GetWindow<GenericMenuExample>().Show();
        }
        
        private void OnGUI()
        {
            var e = Event.current;
            if (e is not null)
            {
                // 右键菜单栏
                if (e.type == EventType.MouseDown && e.button == 1)
                {
                    GenericMenu menu = new GenericMenu();
                    menu.AddItem(new GUIContent("功能1"), false, () => Debug.Log("功能1"));
                    menu.AddItem(new GUIContent("功能合集/功能2"), false, () => Debug.Log("功能2"));
                    menu.AddItem(new GUIContent("功能合集/功能3"), false, () => Debug.Log("功能3"));
                    menu.AddSeparator("功能合集/");
                    menu.AddItem(new GUIContent("功能合集/功能4"), false, () => Debug.Log("功能4"));
                    
                    menu.ShowAsContext();
                }
            }
        }
    }
}